//
//  RCServerCommentManageCell.m
//  RocketProject
//
//  Created by chen jiedan on 13-9-28.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCServerCommentManageCell.h"
#import "UIColor-Expanded.h"
#import "CommonDefines.h"
#import "UIImageView+Extented.h"

#define kStartActiveImageFile    @"image_start_active.png"
#define kStartInActiveImageFile  @"image_merchant_inactive.png"
#define kBGImageFile            @"image_discount_cell_bg.png"

@implementation RCServerCommentManageCell{
    UILabel *_commentLabel;
    UILabel *_nameLabel;
    UILabel *_dateTime;
    
    UILabel *totalAccess;
    UILabel *_totalScore;
    UILabel *cleanAccess;
    UILabel *_cleanScore;
    UILabel *envAccess;
    UILabel *_envScore;
    
    UILabel *_content;
    
    UIImageView *horizonLine;
    
    
    UIButton *_failBtn;
    UIButton *_passBtn;
    int64_t  _reqID;
    
    UIImageView *_startImageView1;
    UIImageView *_startImageView2;
    UIImageView *_startImageView3;
    UIImageView *_startImageView4;
    UIImageView *_startImageView5;
}

@synthesize horizonLine = horizonLine;
@synthesize commentID = _commentID;
@synthesize delegate = _delegate;
@synthesize reviewStatus = _reviewStatus;

- (void) dealloc
{
    _delegate =  nil;
    KF_RELEASE_SAFELY(_commentID);
    [super dealloc];
}



- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]];
        [self setBackgroundColorWithImage:bgImage];
        self.backgroundColor = [UIColor clearColor];
        
        [self createPlaceHolder];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void) createPlaceHolder
{
    _commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 18, 260, 16)];
    [self.contentView addSubview:_commentLabel];
    _commentLabel.font = [UIFont fontWithName:kFontNameHeiti size:16];
    _commentLabel.textColor = [UIColor colorWithHex:0xffff7900];
    _commentLabel.numberOfLines = 0;
    [_commentLabel setBackgroundColor:[UIColor clearColor]];
    [_commentLabel release];
    
    
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 40, 90, 12)];
    [self.contentView addSubview:_nameLabel];
    _nameLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    _nameLabel.textColor = [UIColor colorWithHex:0xff656565];
    [_nameLabel setBackgroundColor:[UIColor clearColor]];
    [_nameLabel release];
    
    
    _dateTime = [[UILabel alloc] initWithFrame:CGRectMake( 95 , 40, 160, 12)];
    [self.contentView addSubview:_dateTime];
    _dateTime.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    _dateTime.textColor = [UIColor colorWithHex:0xff656565];;
    [_dateTime setBackgroundColor:[UIColor clearColor]];
    [_dateTime release];
    
    
    
//    totalAccess = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 55, 15)];
//    [self.contentView addSubview:totalAccess];
//    totalAccess.font = [UIFont fontWithName:kFontNameHeiti size:12];
//    totalAccess.textColor = kNormalTextColor;
//    totalAccess.text = @"总体评价:";
//    [totalAccess setBackgroundColor:[UIColor clearColor]];
//    [totalAccess release];
//    
//    _totalScore = [[UILabel alloc] initWithFrame:CGRectMake(70, 60, 60, 15)];
//    [self.contentView addSubview:_totalScore];
//    _totalScore.font = [UIFont fontWithName:kFontNameHeiti size:12];
//    _totalScore.textColor = kNormalOrangeTextColor;
//    [_totalScore setBackgroundColor:[UIColor clearColor]];
//    [_totalScore release];
    
    _startImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(14, 60, 14, 14)];
    _startImageView1.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_startImageView1];
    
    _startImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(29, 60, 14, 14)];
    _startImageView2.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_startImageView2];
    
    _startImageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(44, 60, 14, 14)];
    _startImageView3.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_startImageView3];
    
    _startImageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(59, 60, 14, 14)];
    _startImageView4.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_startImageView4];
    
    _startImageView5 = [[UIImageView alloc]initWithFrame:CGRectMake(74, 60, 14, 14)];
    _startImageView5.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_startImageView5];
    
//    UIImageView *indicator = [[UIImageView alloc] initWithFrame:CGRectMake(295, 42, 0, 0)];
//    [indicator autoResizeToSetImage:[UIImage imageNamed:@"icon_RCUserVC_indicate"]];
//    [self.contentView addSubview:indicator];
//    [indicator release];
    cleanAccess = [[UILabel alloc] initWithFrame:CGRectMake(94, 60, 55, 15)];
    [self.contentView addSubview:cleanAccess];
    cleanAccess.font = [UIFont fontWithName:kFontNameHeiti size:12];
    cleanAccess.textColor = kNormalTextColor;
    cleanAccess.text = @"环境:";
    [cleanAccess setBackgroundColor:[UIColor clearColor]];
    [cleanAccess release];
    
    _cleanScore = [[UILabel alloc] initWithFrame:CGRectMake(129, 60, 20, 15)];
    [self.contentView addSubview:_cleanScore];
    _cleanScore.font = [UIFont fontWithName:kFontNameHeiti size:12];
    _cleanScore.textColor = kNormalOrangeTextColor;
    [_cleanScore setBackgroundColor:[UIColor clearColor]];
    [_cleanScore release];
    
    
    envAccess = [[UILabel alloc] initWithFrame:CGRectMake(144, 60, 55, 15)];
    [self.contentView addSubview:envAccess];
    envAccess.font = [UIFont fontWithName:kFontNameHeiti size:12];
    envAccess.textColor = kNormalTextColor;
    envAccess.text = @"服务:";
    [envAccess setBackgroundColor:[UIColor clearColor]];
    [envAccess release];
    
    _envScore = [[UILabel alloc] initWithFrame:CGRectMake(174, 60, 20, 15)];
    [self.contentView addSubview:_envScore];
    _envScore.font = [UIFont fontWithName:kFontNameHeiti size:12];
    _envScore.textColor = kNormalOrangeTextColor;
    [_envScore setBackgroundColor:[UIColor clearColor]];
    [_envScore release];
    
    
    _content = [[UILabel alloc] initWithFrame:CGRectMake(14, 75, 210, 20)];
    [self.contentView addSubview:_content];
    _content.font = [UIFont fontWithName:kFontNameHeiti size:12];
    _content.textColor = [UIColor colorWithHex:0xff989797];
    _content.numberOfLines = 0;
    [_content setBackgroundColor:[UIColor clearColor]];
    [_content release];
    
    _failBtn = [[UIButton alloc] initWithFrame:CGRectMake(275, 0, 40, 35)];
    [_failBtn setImage:[UIImage imageNamed:@"icon_comment_fail"] forState:UIControlStateNormal];
    [_failBtn setImage:[UIImage imageNamed:@"icon_comment_fail"] forState:UIControlStateHighlighted];
    [_failBtn addTarget:self action:@selector(failBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_failBtn];
    [_failBtn release];
    
    
    _passBtn = [[UIButton alloc] initWithFrame:CGRectMake(275, 35, 40, 35)];
    [_passBtn setImage:[UIImage imageNamed:@"icon_comment_pass"] forState:UIControlStateNormal];
    [_passBtn setImage:[UIImage imageNamed:@"icon_comment_pass"] forState:UIControlStateHighlighted];
    [_passBtn addTarget:self action:@selector(passBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_passBtn];
    [_passBtn release];
    
    
    
    horizonLine = [[UIImageView alloc]initWithFrame:CGRectMake(0, 70, 320, 1)];
    horizonLine.backgroundColor = [UIColor clearColor];
    [horizonLine setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_line_type3.png" ofType:nil]]];
    [self.contentView addSubview:horizonLine];
    [horizonLine release];
    
}


+ (CGFloat) cellHeightForData:(RCServerCommentManageItem *)item
{
    CGFloat height = 0;
    
    RCServerCommentManageCell *cell = [[RCServerCommentManageCell alloc] init];
    [cell bindData:item];
    height = cell.horizonLine.frame.origin.y + cell.horizonLine.frame.size.height;
    [cell release];
    return height;
}

- (void) bindData:(RCServerCommentManageItem *)item
{
    _commentLabel.text = item.title;
    [_commentLabel sizeToFit];
    
    CGRect nameLabelFrame = _nameLabel.frame;
    nameLabelFrame.origin.y = _commentLabel.frame.origin.y + _commentLabel.frame.size.height + 10;
    _nameLabel.text = item.userName;
    _nameLabel.frame = nameLabelFrame;
    
    CGRect dateTimeFrame = _dateTime.frame;
    dateTimeFrame.origin.y = nameLabelFrame.origin.y;
    _dateTime.text = item.datetime;
    _dateTime.frame = dateTimeFrame;
    

//    CGRect scoreFrame = totalAccess.frame;
//    scoreFrame.origin.y = _dateTime.frame.origin.y + _dateTime.frame.size.height +5;
//    totalAccess.frame = scoreFrame;
//    
//    CGRect scoreCountFrame = _totalScore.frame;
//    scoreCountFrame.origin.y = _dateTime.frame.origin.y + _dateTime.frame.size.height +5;
//    _totalScore.text = item.pinjia;
//    _totalScore.frame = scoreCountFrame;
    
    NSInteger score = [item.pinjia intValue];
    if (score >= 0) {
        
        
        NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
        UIImage *activeImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartActiveImageFile]];
        UIImage *inactiveImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartInActiveImageFile]];
        CGFloat originY = _dateTime.frame.origin.y + _dateTime.frame.size.height +5;
        CGRect rect = CGRectZero;
        
        [_startImageView1 setImage:score > 0 ? activeImage : inactiveImage];
        rect = _startImageView1.frame;
        rect.origin.y = originY;
        _startImageView1.frame = rect;
        
        [_startImageView2 setImage:score > 1 ? activeImage : inactiveImage];
        
        rect = _startImageView2.frame;
        rect.origin.y = originY;
        _startImageView2.frame = rect;
        
        [_startImageView3 setImage:score > 2 ? activeImage : inactiveImage];
        
        rect = _startImageView3.frame;
        rect.origin.y = originY;
        _startImageView3.frame = rect;
        
        [_startImageView4 setImage:score > 3 ? activeImage : inactiveImage];
        
        rect = _startImageView4.frame;
        rect.origin.y = originY;
        _startImageView4.frame = rect;
        
        [_startImageView5 setImage:score > 4 ? activeImage : inactiveImage];
        
        rect = _startImageView5.frame;
        rect.origin.y = originY;
        _startImageView5.frame = rect;
    }
    
    CGRect cleanLabelFrame = cleanAccess.frame;
    cleanLabelFrame.origin.y = _dateTime.frame.origin.y + _dateTime.frame.size.height +5;
    cleanAccess.frame = cleanLabelFrame;
    
    
    CGRect cleanScoreFrame = _cleanScore.frame;
    cleanScoreFrame.origin.y = _dateTime.frame.origin.y + _dateTime.frame.size.height +5;
    _cleanScore.text = item.weisheng;
    _cleanScore.frame = cleanScoreFrame;
    
    CGRect evnFrame = envAccess.frame;
    evnFrame.origin.y = _dateTime.frame.origin.y + _dateTime.frame.size.height +5;
    envAccess.frame = evnFrame;
    
    CGRect evnScoreFrame = _envScore.frame;
    evnScoreFrame.origin.y = _dateTime.frame.origin.y + _dateTime.frame.size.height +5;
    _envScore.text = item.fuwu;
    _envScore.frame = evnScoreFrame;
    
    CGRect contentFrame = _content.frame;
    contentFrame.origin.y = evnScoreFrame.origin.y + evnScoreFrame.size.height + 5;
    _content.frame = contentFrame;
    _content.text = item.content;
    [_content sizeToFit];
    
    CGRect lineFrame = horizonLine.frame;
    lineFrame.origin.y = contentFrame.origin.y + _content.frame.size.height + 18;
    horizonLine.frame = lineFrame;
//    _reserveTimeLabel.text = [NSString stringWithFormat:@"预订时间：%@",item.reversetime];
//    _totalPeople.text = item.personCount;
//    _orderType.text = item.type;
//    _oderPhone.text =[NSString stringWithFormat:@"电话：%@",item.phone];
    
    CGFloat height = horizonLine.frame.origin.y;
    
    if (self.reviewStatus == RCListReviewStatus_UnderReview) {
        _passBtn.hidden = NO;
        _failBtn.hidden = NO;
        
        _failBtn.frame = CGRectMake(275, 0, 40, height/2);
        _passBtn.frame = CGRectMake(275, height/2, 40, height/2);
        
    }else if (self.reviewStatus == RCListReviewStatus_ReviewSuccess){
        _passBtn.hidden = YES;
        _failBtn.hidden = NO;
        _failBtn.frame = CGRectMake(275, (height-40)/2, 40, 40);
    }else if (self.reviewStatus == RCListReviewStatus_ReviewFailed){
        _passBtn.hidden = NO;
        _failBtn.hidden = YES;
        _passBtn.frame = CGRectMake(275, (height-40)/2, 40, 40);
    }
    
    
    self.commentID = item.commentID;
}



- (void) failBtnClick
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    [[IManager sharedInstance] requestUpdateComment:self.commentID withState:CommentState_Fail withDelegate:self];
    
}



- (void) passBtnClick
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
//    [[IManager sharedInstance] requestUpdateImpressive:self.commentID withState:ImpressiveState_Pass withDelegate:self];
    [[IManager sharedInstance] requestUpdateComment:self.commentID withState:CommentState_Pass withDelegate:self];

}


- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if ([_delegate respondsToSelector:@selector(didStateChange:withMsg:)]) {
        [_delegate didStateChange:NO withMsg:@"提交失败"];
    }
}

- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    if (dataModel.errorNO == RCErrorTypeNoError) {
        if ([_delegate respondsToSelector:@selector(didStateChange:withMsg:)]) {
            [_delegate didStateChange:YES withMsg:nil];
        }
    }else{
        if ([_delegate respondsToSelector:@selector(didStateChange:withMsg:)]) {
            [_delegate didStateChange:NO withMsg:dataModel.errorMsg];
        }
    }
}
@end
